﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

using ALE_Server.EC;
using ALE_Server.ECException;

namespace ALE_Server
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

    public class ALEServiceImpl : ALEService, EventListener
    {
        private IDictionary<string,Spec> m_Specs = new Dictionary<string,Spec>();

        #region EventListener 成员

        public void TagEvent(TagEvent te)
        {
            foreach (Spec Spec in  m_Specs.Values)
            {
                Spec.TagEvent(te);
            }
        }

        #endregion

        #region ALEService 成员

        public void Define(string specName, ALE_Server.EC.ECSpec spec)
        {
            if (m_Specs.ContainsKey(specName))
            {
                throw new DuplicateNameException("Spece Name duplicate :" + specName);
            }

            Spec Spec = new Spec(specName, spec);

            m_Specs.Add(specName, Spec);
        }

        public void Undefine(string specName)
        {
            if (!m_Specs.ContainsKey(specName))
            {
                throw new NoSuchNameException("No Such Spec Name :" + specName);
            }

            m_Specs.Remove(specName);
        }

        public ALE_Server.EC.ECSpec GetECSpec(string specName)
        {
            if (!m_Specs.ContainsKey(specName))
            {
                throw new NoSuchNameException("No Such Spec Name :" + specName);
            }

            return m_Specs[specName].ECSpec;
        }

        public IList<string> GetECSpecNames()
        {
            return new List<string>(m_Specs.Keys);
        }

        public void Subscribe(string specName, string NotificationUrl)
        {
            if (!m_Specs.ContainsKey(specName))
            {
                throw new NoSuchNameException("No Such Spec Name :" + specName);
            }

            m_Specs[specName].AddSubscriber(NotificationUrl);
        }

        public void UnSubscribe(string specName, string NotificationUrl)
        {
            if (!m_Specs.ContainsKey(specName))
            {
                throw new NoSuchNameException("No Such Spec Name :" + specName);
            }

            m_Specs[specName].RemoveSubscribers(NotificationUrl);
        }

        public ALE_Server.EC.ECReports Poll(string specName)
        {
            if (!m_Specs.ContainsKey(specName))
            {
                throw new NoSuchNameException("No Such Spec Name :" + specName);
            }

            return Immediate(m_Specs[specName].ECSpec);
        }

        public ALE_Server.EC.ECReports Immediate(ALE_Server.EC.ECSpec spec)
        {
            throw new NotImplementedException();
        }

        public IList<string> GetSubscribers(string specName)
        {
            if (!m_Specs.ContainsKey(specName))
            {
                throw new NoSuchNameException("No Such Spec Name :" + specName);
            }

            return m_Specs[specName].Subscribers;
        }

        public string GetStrandardVersion()
        {
            return "ALE 1.0";
        }

        public string GetVendorVersion()
        {
            return "DMET of HUST in China.";
        }

        #endregion
    }
}
